package com.bookstore.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtils {
    public static Cookie findByName(Cookie[] cookies, String name) {
        if (name == null) {
            return null;
        }
        for (int i = 0; i < cookies.length; i++) {
            if (name.equals(cookies[i].getName())) {
                return cookies[i];
            }
        }
        return null;
    }

    public static void addCookies(HttpServletRequest request, HttpServletResponse response, Cookie... cookies) {
        for (int i = 0; i < cookies.length; i++) {
            response.addCookie(cookies[i]);
        }
    }

    public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, Cookie cookie) {
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}
